﻿using Basic.FormCollect;
using Basic.FormModel.Form;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Form;
using Basic.FormRemoteModel.Form.Model;
using Basic.FormService.Interface;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.Helper;

namespace Basic.FormService.RpcEvent
{
    internal class FormStructureEvent : IRpcApiService
    {
        private readonly IFormColSelectService _Service;
        private readonly IPhyFormColSelectService _PhySelect;
        private readonly ICustomFormCollect _Form;
        private readonly IPhyQueryColSelectService _PhyQuery;
        private readonly IFormViewColService _ViewCol;
        public FormStructureEvent ( IFormColSelectService service,
            IPhyFormColSelectService phySelect,
            IPhyQueryColSelectService phyQuery,
            IFormViewColService viewCol,
            ICustomFormCollect form )
        {
            this._ViewCol = viewCol;
            this._PhyQuery = phyQuery;
            this._Service = service;
            this._PhySelect = phySelect;
            this._Form = form;
        }

        public StructureColumn[] GetFormStructures ( GetFormStructures obj )
        {
            List<StructureColumn> cols = new List<StructureColumn>();
            FormBaseDto[] forms = this._Form.Gets<FormBaseDto>(obj.FormId);
            return forms.ConvertAll(a =>
            {
                StructureColumn col = new StructureColumn
                {
                    Id = a.Id,
                    Label = a.FormName,
                    Type = StructureType.Form,
                };
                if ( a.FormType == FormType.代理表单 )
                {
                    col.Children = this._PhySelect.Gets(a.Id, obj.Param);
                }
                else if ( a.FormType == FormType.物理表单 )
                {
                    //obj.Param.ShowMode = new PhyColShowMode[] { PhyColShowMode.Column };
                    col.Children = this._PhyQuery.Gets(a.Id, obj.Param);
                }
                else if ( a.FormType == FormType.视图 )
                {
                    col.Children = this._ViewCol.GetColumns(a, obj.Param);
                }
                else
                {
                    col.Children = this._Service.Gets(a.Id, obj.Param);
                }
                return col;
            });
        }
        public StructureColumn GetFormStructure ( GetFormStructure obj )
        {
            FormBaseDto form = this._Form.Get<FormBaseDto>(obj.FormId);
            StructureColumn col = new StructureColumn
            {
                Id = form.Id,
                Label = form.FormName,
                Type = StructureType.Form,
            };
            if ( form.FormType == FormType.代理表单 )
            {
                col.Children = this._PhySelect.Gets(obj.FormId, obj.Param);
            }
            else if ( form.FormType == FormType.物理表单 )
            {
                col.Children = this._PhyQuery.Gets(obj.FormId, obj.Param);
            }
            else if ( form.FormType == FormType.视图 )
            {
                col.Children = this._ViewCol.GetColumns(form, obj.Param);
            }
            else
            {
                col.Children = this._Service.Gets(obj.FormId, obj.Param);
            }
            return col;
        }
    }
}
